package com.xmmj.baselibrary.base.fragment

import androidx.annotation.IdRes
import android.annotation.SuppressLint
import android.app.Activity
import android.view.View
import androidx.fragment.app.Fragment
import com.trello.rxlifecycle3.components.support.RxFragment
import java.lang.Exception
import java.lang.reflect.Field

abstract class LibBaseFragment : LazyFragment() {

    companion object {
        /**
         * 获取手机状态栏高度
         */
        @SuppressLint("PrivateApi")
        fun getStatusBarHeight(activity: Activity): Int {
            val c: Class<*>
            val obj: Any
            val field: Field
            var x = 0
            var statusBarHeight = 0
            try {
                c = Class.forName("com.android.internal.R\$dimen")
                obj = c.newInstance()
                field = c.getField("status_bar_height")
                x = field[obj].toString().toInt()
                statusBarHeight = activity.resources.getDimensionPixelSize(x)
            } catch (e: Exception) {
                e.printStackTrace()
            }
            return statusBarHeight
        }
    }

    /**
     * 显示控件
     *
     * @param views 控件
     */
    fun visible(vararg views: View) {
        for (view in views) {
            view.visibility = View.VISIBLE
        }
    }

    /**
     * 隐藏控件
     *
     * @param views 控件
     */
    fun invisible(vararg views: View) {
        for (view in views) {
            view.visibility = View.INVISIBLE
        }
    }

    /**
     * 隐藏控件
     *
     * @param views 控件
     */
    fun gone(vararg views: View) {
        for (view in views) {
            view.visibility = View.GONE
        }
    }

    /**
     * 添加 fragment
     *
     * @param idRes    容器ID
     * @param fragment 待添加的 fragment
     */
    fun addFragment(@IdRes idRes: Int, fragment: RxFragment?) {
        val ft = requireActivity().supportFragmentManager.beginTransaction()
        ft.add(idRes, fragment!!)
        ft.commit()
    }

    /**
     * 替换 fragment
     *
     * @param idRes    容器id
     * @param fragment 目标fragment
     */
    fun replaceFragment(@IdRes idRes: Int, fragment: RxFragment?) {
        val ft = requireActivity().supportFragmentManager.beginTransaction()
        ft.replace(idRes, fragment!!)
        ft.commit()
    }

    /**
     * 隐藏 fragment
     *
     * @param fragment 目标fragment
     */
    fun hideFragment(fragment: RxFragment?) {
        val ft = requireActivity().supportFragmentManager.beginTransaction()
        ft.hide(fragment!!)
        ft.commit()
    }

    /**
     * 显示 fragment
     *
     * @param fragment 目标fragment
     */
    fun showFragment(fragment: RxFragment?) {
        val ft = requireActivity().supportFragmentManager.beginTransaction()
        ft.show(fragment!!)
        ft.commit()
    }

    /**
     * 设置当前Fragment
     *
     * @param id       容器id
     * @param fragment 目标fragment
     */
    fun setCurrentFragment(id: Int, fragment: Fragment) {
        val fragments = requireActivity().supportFragmentManager.fragments
        val fragmentTransaction = requireActivity().supportFragmentManager.beginTransaction()
        if (!fragment.isAdded) fragmentTransaction.add(id, fragment, fragment.toString())
        for (i in fragments.indices) {
            val f = fragments[i]
            if (f.tag != fragment.id.toString() + "") fragmentTransaction.hide(f)
        }
        fragmentTransaction.show(fragment)
        fragmentTransaction.commitAllowingStateLoss()
        requireActivity().supportFragmentManager.executePendingTransactions()
    }
}